﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Data;
using Common;

namespace CSCV.Group1.NewsAnnouncement.PagingList
{
    public partial class PagingListUserControl : UserControl
    {
        public string _Pagination = "";
        public DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData(1);
            }
        }

        private void LoadData(int currentPage)
        {
            ViewState["CurrentPage"] = currentPage;
            FillData(ViewState["Next"] as string);
        }

        private void FillData(string pagingInfo)
        {
            int currentPage = Convert.ToInt32(ViewState["CurrentPage"]);
            //string nextPageString = "Paged=TRUE&p_ID={0}";
            //string PreviousPageString = "Paged=TRUE&PagedPrev=TRUE&p_ID={0}";
            SPListItemCollection collection;
            int rowCount = 2;
            int totalRow = 0;

            //first make a call to fetch the desired result set
            //here is the actual call to the dal function
            collection = DataAccess.GetTestItems(pagingInfo, uint.Parse(rowCount.ToString()), out totalRow);
            dt = collection.GetDataTable();
            //spGridView.DataSource = objDataTable;
            //spGridView.DataBind();

            //now we need to identify if this is a call from next or first

            //if (null != collection.ListItemCollectionPosition)
            //    nextPageString = string.Format(nextPageString, collection[collection.Count - 1].ID);
            //else
            //    nextPageString = string.Empty;

            //if (currentPage > 1)
            //    PreviousPageString = string.Format(PreviousPageString, collection[0].ID);
            //else
            //    PreviousPageString = string.Empty;
            
            //ViewState["Previous"] = PreviousPageString;
            //ViewState["Next"] = nextPageString;


            int totalPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalRow) / Convert.ToDouble(rowCount)));
            

            Paging p = new Paging();
            p.BaseUrl = new bzParameter().currentPage;  //link cua trang
            p.CurPage = currentPage;      //focus trang hien tai
            p.TotalRows = totalRow;  // tong so record   
            p.PerPage = rowCount;     // gioi han so item tren 1 trang  
            p.NumLinks = 3;     // so item hien thi ben trai va ben phai. 5x2=10;

            _Pagination = p.GetPageLinks(PagingType.DefaultWithItemLeft);
        }

    }
}
